/**
 * Created by lixiaoping on 15/10/23.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'model',
    'dialog'
], function ($, common, ready, template, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            this.model = new Model({
                container: 'main-view',
                template: 'order-tpl',
                tag_name: 'div',
                url: _global.url.api + 'order/' + id,
                callback: function () {
                    loaded();
                    if (this.result.code == 0 && this.result.data.type == 6) {
                        //把京东物流加载出来
                        $.ajax({
                            url: 'http://api.sixishop.com/get_jd_order_shipping_info?key=121&id=' + id,
                            type: 'get',
                            async: false,
                            success: function (res) {
                                if (res.ret == 0) {
                                    var info = res.info;
                                    for (var i = 0; i < info.length; i++) {
                                        $('#jd_shipping_info').append('<p>' + info[i].info + ',' + info[i].time + '</p>');
                                    }
                                }
                            }
                        });
                    }
                }
            });
            this.model.fetch();
        },

        event: function () {
            var _this = this;
            console.log(this.model.data);
            /**
             * 添加事件
             */
            $('body').on('click', '#btn_dec_shipping_fee', function (e) {
                e.preventDefault();
                dialog({
                    title: '修改邮费',
                    content: template('edit_shipping_fee-tpl', _this.model.data),
                    width: 500,
                    ok: function () {
                        var data = {};
                        data.action = 'editShippingFee';
                        data.shipping_fee = $('[name="shipping_fee"]').val();
                        if (new Model({
                                url: _global.url.api + 'order/' + id,
                                data: data
                            }).put()) {
                            _this.fetch();
                        }
                    }, cancel: function () {
                    }
                }).show();
            });
            var shipping_kuaidi_data = [];
            $('body').on('click', '#btn_shipped,#btn_edit_shipped', function (e) {
                e.preventDefault();
                dialog({
                    title: '发货',
                    content: template('shipping-tpl', _this.model.data),
                    width: 500,
                    ok: function () {
                        var data = {};
                        data.action = 'deliver';
                        data.shipping_no = $('[name="shipping_no"]').val();
                        data.shipping_note = $('[name="shipping_note"]').val();
                        data.express_id = $('[name="express_id"]').val();
                        if (new Model({
                                url: _global.url.api + 'order/' + id,
                                data: data
                            }).put()) {
                            _this.fetch();
                        }
                    }, cancel: function () {
                    }
                }).show();
                if (shipping_kuaidi_data.length < 1) {
                    var model = new Model({
                        url: _global.url.api + 'orderExprecssList'
                    });
                    model.fetch();
                    shipping_kuaidi_data = model.data;
                }
                $.each(shipping_kuaidi_data, function (i, item) {
                    $("#shipping_kuaidi_id").append("<option value='" + item.id + "' " + item.is_selected + ">" + item.name + "</option>");
                });
                if (_this.model.data.express_id > 0) {
                    $("#shipping_kuaidi_id").val(_this.model.data.express_id);
                }
            });
        }
    };

    new index();

});